﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Common;
using Module;
using EMPower.Utility;

namespace App
{
    public partial class PolicyPager_PolicyPager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal commentuserinfo = (Literal)e.Item.FindControl("commentuserinfo");
                Literal labcommentbody = (Literal)e.Item.FindControl("labcommentbody");
                PostInfo p = e.Item.DataItem as PostInfo;

                if (p != null && p.PostId > 0)
                {
                    if (commentuserinfo != null)
                    {
                        commentuserinfo.Text = "<a href='#'>" + (string.IsNullOrEmpty(p.NickName) ? p.UserName : p.NickName.ToString()) + "</a> 发表于" + p.DateCreated.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    if (labcommentbody != null)
                    {
                        labcommentbody.Text = p.Body;
                    }
                }


            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptCommentList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void btn_OnClick(object sender, EventArgs e)
        {
            if (AppAuthHelper.SessionCheck())
            {
                long paperId = 0;
                if (long.TryParse(this.hf_PaperId.Value, out paperId))
                {
                    PostInfo postInfo = new PostInfo();
                    postInfo.Body = this.content.Value;
                    postInfo.DateCreated = DateTime.Now;
                    postInfo.LastUpdate = postInfo.DateCreated;
                    postInfo.PaperId = paperId;
                    postInfo.PostId = IdHelper.GetLongId();
                    postInfo.Status = 1;
                    postInfo.ToPostId = 0;
                    postInfo.ToUserPassportId = 0;
                    postInfo.UserPassportId = AppAuthHelper.CurrentUserPassportId;

                    if (BLL.BPost.getInstance().Insert(postInfo))
                    {

                        Community.Utility.JavaScriptHelper.MessageBox(this, "发表评论成功");
                        this.content.Value = string.Empty;
                        bindPost();
                    }
                    else
                    {
                        Community.Utility.JavaScriptHelper.MessageBox(this, "发表评论失败,请重试");
                    }

                }
                else
                {
                    Community.Utility.JavaScriptHelper.MessageBox(this, "请刷新页面重试");
                }
            }
            else
            {
                Community.Utility.JavaScriptHelper.ConfirmBox(this, "请登录后发表评论，确定登录？", AppAuthHelper.GetLoginUrl(), Contexts.ThisPageUrlWithParams);
            }
        }
        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            bindPost();
        }
        private void pageInit()
        {
            long paperId = Contexts.GetAPPPaperId("/PolicyPager/PolicyPagerList.aspx");

            PaperInfo paper = BLL.BPaper.getInstance().GetPaperInfo(paperId);

            if (paper != null && paper.PaperId > 0 && paper.Status == 1)
            {
                lbl_Subject.Text = paper.Subject;
                lbl_DateTime.Text = paper.DateCreated.ToShortDateString();
                lbl_Body.Text = paper.Body;
                hf_PaperId.Value = paper.PaperId.ToString();
                if (paper.IsAllowComment == 0)
                {
                    divComment.Visible = false;
                    divClientNoSee.Visible = false;

                }
                else
                {
                    divComment.Visible = true;
                    divClientNoSee.Visible = true;

                    //
                    bindPost();
                }


            }
            else
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "文章失效，即将回退到上级页面");
                Page.RegisterClientScriptBlock("", "<script>widow.location='" + Contexts.AppPath + "/PolicyPager/PolicyPagerList.aspx'</script>");
            }
        }


        private List<PostInfo> getPages(long paperId)
        {
            List<PostInfo> posts = BLL.BPost.getInstance().GetAllPostANDUserNameByPaperId(paperId);

            if (posts != null && posts.Count > 0)
            {
                this.anpOrders.RecordCount = posts.Count;
                List<PostInfo> tempPosts = new List<PostInfo>();
                tempPosts.AddRange(posts);
                int currentNum = 0;

                //分页数据处理
                foreach (PostInfo info in tempPosts)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        posts.Remove(info);
                    }
                }
            }

            return posts;
        }

        private void bindPost()
        {
            long paperId = 0;
            if (long.TryParse(this.hf_PaperId.Value, out paperId))
            {
                List<PostInfo> posts = getPages(paperId);
                if (posts != null)
                {
                    this.rptCommentList.DataSource = posts;
                    this.rptCommentList.DataBind();
                }
            }
            else
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "请刷新页面重试");
            }
        }
    }
}